0

第一个脚本在单击时将文本转换为可编辑的文本字段。编辑文本后,按下激活第二个脚本的提交按钮。第二个脚本应该通过 AJAX 将一些信息发送到另一个页面,最终提交到数据库中。前两个变量(projectID 和contactType)通过AJAX 发送就好了,但是updatedInfo(应该包含编辑文本的变量)总是空白。我假设这是我想念的愚蠢的东西,但我无法弄清楚。两个脚本如下。

<script type="text/javascript">
    $(document).ready(function() {
        var alreadyActive = "";
        $(".editable").on("click", function () {
            if(!alreadyActive) {
                OriginalText = $(this).text();
                divValue = $(this).attr('value');
                $(this).html("<form class='edit-form' name='" + divValue + "'><input type='text' value='" + OriginalText + "' /><input type='submit' value='Update' /></form>");
                alreadyActive = "true";
            }
        });
    });
</script>
<script type="text/javascript">
    $('body').delegate('.edit-form','submit',function(e){
        e.preventDefault();
        var projectID = <?php echo $projectID ?>;
        var contactType = $(this).attr('name');
        var updatedInfo = $(this).val();

        $.ajax({
            url: 'editinplace.php?projectID=' + projectID + '&contactType=' + contactType + '&updatedInfo=' + updatedInfo,
        });
    });
</script>
4

2 回答 2

6

如果您试图获取表单内文本字段的值,那么您想要更改:

var updatedInfo = $(this).val();

到:

var updatedInfo = $(this).find('input[type="text"]:first').val();

What your code is doing is looking for a value attribute on the <form> tag itself. Instead, since the value you're after is located in the <input> tag, you have to get jQuery to go look at that specific tag.

于 2012-11-09T19:56:56.613 回答
0

it actually looks like you are getting the 'value' of the form

var updatedInfo = $(this).val();

'this' (if i am reading the code correctly) would be the form, not the input. and like mentioned by the others answering to get the text of a button try $(this).text()

于 2012-11-09T20:03:31.840 回答