Pass multiple values with onClick in HTML link

后端 未结 8 1578
眼角桃花
眼角桃花 2021-02-04 03:50

Hi Im trying to pass multiple values with the HTML onclick function. Im using Javascript to create the Table

var user = element.UserName;
var valuationId = eleme         


        
相关标签:
8条回答
  • 2021-02-04 04:06
     $Name= "'".$row['Name']."'";
      $Val1= "'".$row['Val1']."'";
      $Year= "'".$row['Year']."'";
      $Month="'".$row['Month']."'";
    
     echo '<button type="button"   onclick="fun('.$Id.','.$Val1.','.$Year.','.$Month.','.$Id.');"  >submit</button>'; 
    
    0 讨论(0)
  • 2021-02-04 04:07

    If valuationId and user are JavaScript variables, and the source code is plain static HTML, not generated by any means, you should try:

    <a href=# onclick="return ReAssign(valuationId,user)">Re-Assign</a>
    

    If they are generated from PHP, and they contain string values, use the escaped quoting around each variables like this:

    <?php
        echo '<a href=# onclick="return ReAssign(\'' + $valuationId + '\',\'' + $user + '\')">Re-Assign</a>';
    ?>
    

    The logic is similar to the updated code in the question, which generates code using JavaScript (maybe using jQuery?): don't forget to apply the escaped quotes to each variable:

    var user = element.UserName;
    var valuationId = element.ValuationId;
    $('#ValuationAssignedTable').append('<tr> <td><a href=# onclick="return ReAssign(\'' + valuationId + '\',\'' + user + '\')">Re-Assign</a> </td>  </tr>');
    

    The moral of the story is

    'someString(\''+'otherString'+','+'yetAnotherString'+'\')'
    

    Will get evaluated as:

    someString('otherString,yetAnotherString');
    

    Whereas you would need:

    someString('otherString','yetAnotherString');
    
    0 讨论(0)
  • 2021-02-04 04:07

    That is because you pass string to the function. Just remove quotes and pass real values:

    <a href=# onclick="return ReAssign(valuationId, user)">Re-Assign</a>
    

    Guess the ReAssign function should return true or false.

    0 讨论(0)
  • 2021-02-04 04:09
    function ReAssign(valautionId, userName) {
       var valautionId 
       var userName 
     alert(valautionId);
     alert(userName);
      }
    
    <a href=# onclick="return ReAssign('valuationId','user')">Re-Assign</a>
    
    0 讨论(0)
  • 2021-02-04 04:10

    Please try this

    for static values--onclick="return ReAssign('valuationId','user')"
    for dynamic values--onclick="return ReAssign(valuationId,user)"
    
    0 讨论(0)
  • 2021-02-04 04:12

    Solution: Pass multiple arguments with onclick for html generated in JS

    For html generated in JS , do as below (we are using single quote as string wrapper). Each argument has to wrapped in a single quote else all of yours argument will be considered as a single argument like functionName('a,b') , now its a single argument with value a,b.

    We have to use string escape character backslash() to close first argument with single quote, give a separator comma in between and then start next argument with a single quote. (This is the magic code to use '\',\'')

    Example:

    $('#ValuationAssignedTable').append('<tr> <td><a href=# onclick="return ReAssign(\'' + valuationId  +'\',\'' + user + '\')">Re-Assign</a> </td>  </tr>');
    
    0 讨论(0)
提交回复
热议问题