在querySelector

时间:2018-09-21 23:49:47

标签: javascript php

这可能是基本的,但是我已经半天都在搜索有效的答案。我有一个在while语句中动态创建的表单(返回许多表单)。我正在使用jQuery来获取表单名称,并且每个表单都需要一个唯一的名称,否则它只能在第一个表单上使用。

我创建了一个动态表单名称,每次迭代的末尾都有一个数字,该数字可以很好地工作,从而为每个表单创建一个唯一的表单名称。现在,我想在querySelector中使用变量-尽管它需要一个字符串。如何在每个循环中将变量作为字符串传递?如果用引号引起来,则使用键入的字符串;如果使用变量,则将其破坏。

var form_name = <?php echo '#'.$form_name; ?>;
document.querySelector(**form_name**).addEventListener('submit', function(e) {
var form = this;
more code…

这是完整的代码(大部分)

                $watch_num = 0;
                while ($row_watched = mysqli_fetch_row($result_watched))
                {
                    $watch_num = $watch_num + 1;
                    $delete_name = "delete_form".$watch_num;

                    echo '<table width="570" border="0" cellspacing="0" cellpadding="4"'.$background.'>
                      <tr>
                        <td width="148" rowspan="5" align="center" valign="top"><img src="'.$image.'" vspace="4" hspace="6" width="140" height="105" style="padding-top:3px" /></td>
                        <td>Item Number:'.$number.'</td><td align="right">'.$featured.'</td>
                        </tr>
                      <tr>
                        <td colspan="2"><strong>Title: '.$title.'</strong></td>
                        </tr>
                      <tr>
                        <td width="222">Trader: '.$lister.'</td>
                        <td width="176">'.$type.'</td>
                        </tr>
                      <tr>
                        <td>'.$time_status.'</td>
                        <td>'.$price.'</td>
                        </tr>
                      <tr>
                        <td>';
                        if($status == "A" || $status == "X" || $status == "C"){
                        echo '<input name="other" type="button" class="grn_button_small2" id="other" value="More From Seller" onclick="window.location=\'seller.php?sellerid='.$lister.'\'"  />';
                        }

                        if($status == "A"){
                        echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="details" type="button" class="grn_button_small2" id="details" value="Details" onclick="window.location=\'details.php?itemid='.$item_id.'\'"  /></td>';
                        }
                        echo '<td><form name="'.$delete_name.'" method="post" action="" id="delete_form">
                            <input type="hidden" id="item" name="item" value="'.$item_id.'" />
                            <input type="submit" class="grn_button_small2" id="delete_btn" name="delete_btn" value="Stop Watching"/>  
                            </form>';
                            echo '</td>
                        </tr>
    ?>
<script>

     jQuery(document).ready(function($) {
var form_name = <?php echo json_encode("#$delete_name"); ?>; 
document.querySelector(form_name).addEventListener('submit', function(e) {
  var form = this;
      var stop_abort = "\nYou didnt stop it";

      e.preventDefault();
      swal({
          title: "Stop",
          text: "Are you sure you want to stop?",
          closeOnClickOutside: false,
          dangerMode: true,
          buttons: [
            'Cancel',
            'Stop'
          ],
        }).then(function(isConfirm) {
          if (isConfirm) {
        swal({
          title: 'Cancelled!',
          text: 'Stopped',
          //icon: 'success'
        }).then(function() {
          form.submit(); // <--- submit form programmatically
        });
      } else {
            swal("Stop  Aborted!", stop_abort);
          }
        });
    });
});
</script>
                <?php
                      </table><hr align="center" color="#D4D8E1" noshade="noshade" />';
         }//end while
        ?>

2 个答案:

答案 0 :(得分:1)

确保您引用的是价值。一种简单的方法是使用json_encode,它可以避免转义任何需要转义的值。

var form_name = <?=json_encode("#$form_name")?>;
document.querySelector(form_name).addEventListener('submit', function(e) {
    var form = this;
    //...
});

答案 1 :(得分:0)

你有这个:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-1-9132fbd19d75> in <module>()
      1 import pickle
----> 2 pickle_in = 
open(r"C:/Users/manas/PycharmProjects/allProjects/X.pickle","rb")
      3 X = pickle.load(pickle_in)
      4 
      5 pickle_in = 
open(r"C:/Users/manas/PycharmProjects/allProjects/y.pickle","rb")

FileNotFoundError: [Errno 2] No such file or directory: 
'C:/Users/manas/PycharmProjects/allProjects/X.pickle'



import pickle
pickle_in = 
open(r"C:/Users/manas/PycharmProjects/allProjects/X.pickle","rb")
X = pickle.load(pickle_in)

pickle_in = 
open(r"C:/Users/manas/PycharmProjects/allProjects/y.pickle","rb")
Y = pickle.load(pickle_in)

您应该在哪里:

<form name="'.$delete_name.'" method="post" action="" id="delete_form">

甚至是这个:

<form name="'.$delete_name.'" method="post" action="" id="'.$delete_name.'">

有两个原因:

  • 您不能有多个具有相同ID的HTML元素,
  • <form id="'.$delete_name.'" method="post"> <!-- No need for name, nor action --> document.querySelector一起使用时,它是指ID,而不是名称。

有关其余信息,请参见@ miken32的答案。

不过,作为一般说明,生成这样的ID是一种不好的做法,并且不需要99%的时间。您可以为所有表单指定一个特定的类,或者更好的是,为特定的#something属性提供一个属性,然后使用jQuery或data-一次查询所有表单(您似乎将这两种语法混合使用)。