从javascript值变量中检索整个字符串

时间:2018-03-19 12:34:54

标签: javascript jquery

我试图将一个大字符串保存为javascript变量中的值,问题是只能在javascript变量中保存字符串的第一个单词,我的源代码如下:

当用户点击按钮时调用JS函数,然后按钮的值("变量的值是大字符串")存储在javascript变量中,然后以html格式显示。 html表单的源代码如下:



var input; //prepare var to save large string
    $(function() {
     // contact form animations
     $('button[id="contactbutton"]').click(function() {
     input = $(this).val(); //set var input to value of the pressed button
       document.getElementById("someInput").value = input;
        console.log('input : ' + input);
        $('#contactForm').fadeToggle();
      })
      $(document).mouseup(function (e) {
        var container = $("#contactForm");
    
        if (!container.is(e.target) // if the target of the click isn't the container...
            && container.has(e.target).length === 0) // ... nor a descendant of the container
        {
            container.fadeOut();
          
        }
      });
    
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contactForm" >
                      <p><h4><i>First Choose the clients and then the file which will be uploaded in order to proced</i></h4></2>
                      <hr>
                      <input type="text" id="someInput" name="someInput" readonly></input>
                      <hr>
                      </div>
&#13;
&#13;
&#13;

按钮显示在html表格中,表格使用mysql查询拖动数据并在网站中显示。然后创建一个仅包含按钮的添加列,每个按钮被赋予不同的值,该表的源代码如下:

 $con = mysql_connect("localhost","root",'');
   if(!$con){

   die("Cannot Connect" . mysql_error());

   }
  mysql_select_db("client_app",$con);
  $get_notifications= "SELECT * FROM `notifications history` ";
  $notifications = mysql_query($get_notifications,$con);

   echo "<table class=table table-condensed>
   <thead>
   <tr>   
   <th>Recipient</th>
   <th>Title</th>
   <th>Message</th>
   <th>Time Sended</th>
   </tr>
   </thead>";
   while($record = mysql_fetch_array($notifications)){
    echo "<action=usersfiles.php method=post>";
    echo "<tr>";
    echo "<td>".$record['recipient']." </td>";
    echo "<td>".$record['title']." </td>";
      echo "<td>"."<button   value=".$record['content']." id=contactbutton>Content</button>"." </td>";
    echo "<td>".$record['received']." </td>";
    echo "</tr>";
    echo "</form>";

     }

echo "</table>";     
mysql_close();

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这一行有几个问题:

  echo "<td>"."<button   value=".$record['content']." id=contactbutton>Content</button>"." </td>";

首先,你说你只看到字符串的第一个单词。这是因为您没有引用value属性,如果值包含空格,则需要引号。其次,所有按钮都具有相同的ID,这是无效的HTML。如果您在记录中有一个id字段,我建议将其添加到按钮的id中。

我建议按如下方式更改该行。请注意,如果内容包含双引号,则在内容周围使用addslashes()。如果您在记录中没有id字段,则需要找到使按钮ID唯一的其他方法。

  echo "<td><button value=\"". addslashes($record['content']) . "\" id=\"contactbutton" . $record['id'] . "\">Content</button>"." </td>";