简体   繁体   English

如何使用jquery获取编辑文本的价值

[英]How to get value of edit text with jquery

Hi i want to get changed text value from JQuery but i can't select edit text with JQUERY because edit texts generated from php while loop that this php code query on database and get value of edit texts and in my program i have edit button for every edit texts and when the user changed value of edit text i select new value and when user click edit button send this value from get method to another php page with jquery $.ajax function and send new value to that php code with ajax.But i don't know how can i select edit text that it's value changed because i don't know id of that edit text!.And when i set one id for every edit text i only get first edit text value from $("#id").change().val(); 嗨,我想从JQuery中获取更改的文本值,但我不能选择使用JQUERY编辑文本,因为编辑从php生成的文本同时循环,这个PHP代码在数据库上查询并获取编辑文本的值,在我的程序中我有编辑按钮每个编辑文本,当用户更改编辑文本的值时,我选择新值,当用户单击编辑按钮时,将此值从get方法发送到另一个带有jquery $.ajax函数的php页面,并使用ajax.But将新值发送到该PHP代码我不知道如何选择编辑文本它的值已更改,因为我不知道该编辑文本的ID!。当我为每个编辑文本设置一个id时,我只从$("#id").change().val();获得第一个编辑文本值$("#id").change().val(); .I use below code but it doesn't work.I am beginner in java script and don't know how fix this problem!. 。我使用下面的代码,但它不起作用。我是java脚本的初学者,不知道如何解决这个问题!

var testAnswer;

function setNewTestAnswer(id){
    testAnswer = $("#id").val();
}
function sendToEdit(pID,phID,thDate,type){
    var info = 'pId='+pID+'&phId='+phID+'&testAnswer='+testAnswer+'&thDate='+thDate+'&type='+type;
}

2nd function use testAnswer that user changed in edit text. 第二个功能使用testAnswer用户在编辑文本中更改。
php code PHP代码

<?php
    include 'Connect.php'; 
    if(match($_POST['pId'], "/^[\d]+$/") ){
        $pId = $_POST['pId'];
        $result = mysql_query("select pName, pID, phName, phID, testHistoryDate, type, testAnswer from patient join reception using(pID) join physician using(phID) join testHistory using(rID) join test using(tID) where pID = $pId",$connection);
    }
    else
        die("Insert true value");
    while($row=mysql_fetch_array($result)){
        echo "<tr><td>";
        echo $row["pName"].'</td>';
        echo '<td>'.$row["phName"].'</td>';
        echo '<td>'.$row["testHistoryDate"].'</td>';
        echo '<td>'.$row["type"].'</td>';
        $type =  $row['type'];
        $testHistoryDate = $row['testHistoryDate'];
        ?>
        <td>
            <span id='spryTanswer'>
                <input type='text' name='tAnswer' id='tAnswer' value='<?php echo $row['testAnswer']; ?>' />
            </span>
        </td>
        <td>
            <input type='submit' value='Edit' name='edit' id='edit' onclick="sendToEdit('<?php echo $row['pID'] ?>','<?php echo $row['phID'] ?>', '<?php echo $row['testHistoryDate'] ?>', '<?php echo $row['type'] ?>')" />
        </td>
        </tr>
    <?php } ?>

tl;dr TL;博士

So it isn't completely clear what you are trying to do here but I can explain a couple things that might help. 所以你在这里尝试做什么并不完全清楚,但我可以解释一些可能有帮助的事情。

  • in html ids should be unique. 在HTML ids中应该是唯一的。 You dont have to obey this rule for your page to work but you have found one of the consequences if breaking it: jQuery will only find the first one. 您不必遵守此规则以使您的页面正常工作,但是如果破坏它,您会发现其中一个后果:jQuery只会找到第一个。

  • it is a good idea to base html ids on some unique attribute of your data eg the table row id. 将html id基于数据的某些唯一属性(例如表行id)是个好主意。

  • You can get more creative with your jQuery selectors for example 例如,您可以使用jQuery选择器获得更多创意

    $('input[type="text"]') // gets all the text inputs

  • Use classes. 使用类。 When you want to be able to easily select all of a group of html elements you should give them all the same class name. 当您希望能够轻松选择所有一组html元素时,您应该为它们提供相同的类名。 one element can have multiple class names and many elements can share a class name. 一个元素可以有多个类名,许多元素可以共享一个类名。 you can then select them by class name using jquery like this: 然后你可以使用jquery按类名选择它们,如下所示:

    $('.myclassname')

I think you need to change your php to look more like this: 我认为你需要改变你的PHP看起来更像这样:

<span class='spryTanswer'>
  <input type='text' name='tAnswer' id='tAnswer-<?php echo $row['id'] ?>' value='<?php echo $row['testAnswer']; ?>' />
</span>

Since you're creating elements inside a php loop, you must be sure that every element has a unique id (or no id at all). 由于您在php循环中创建元素,因此您必须确保每个元素都具有唯一ID(或根本没有id)。 You can use either an incrementing index, or some unique value in your array. 您可以使用递增索引或数组中的某个唯一值。 At first glance, seems that $row['pID'] is a good candidate: 乍一看,似乎$row['pID']是一个很好的候选者:

<input id='edit_<?php $row['pID'] ?>' type='submit' value='Edit' ... />

After that you should be able to target individual elements. 之后,您应该能够定位单个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM