Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Jquery将值放入输入字段?_Jquery - Fatal编程技术网

如何使用Jquery将值放入输入字段?

如何使用Jquery将值放入输入字段?,jquery,Jquery,使用ajax,这个数字在phpecho$response中得到响应然后在Jquery中,我想把这个数字放在一个名为“total one”的字段中。我该怎么做 迄今为止的代码: php文件中的 echo $response; jQuery(document).ready(function($) { (function($) { $(document).ready(function() { $('#formsubmit').click(functio

使用ajax,这个数字在php
echo$response中得到响应然后在Jquery中,我想把这个数字放在一个名为“total one”的字段中。我该怎么做

迄今为止的代码:

php文件中的

echo $response;
jQuery(document).ready(function($) {
    (function($) {
        $(document).ready(function() {
            $('#formsubmit').click(function() {
                $.post(
                PT_Ajax.ajaxurl, {
                    action: 'ajax-inputtitleSubmit',
                    numberofwelds: $('input[name=numberofwelds]').val(),
                    numberofconwelds: $('input[name=numberofconwelds]').val(),
                    nextNonce: PT_Ajax.nextNonce
                }, function(response) {
                    $('#totalone').val(response);
                });
                return false;
            });
        });
    })(jQuery);
});
Jquery文件中的

echo $response;
jQuery(document).ready(function($) {
    (function($) {
        $(document).ready(function() {
            $('#formsubmit').click(function() {
                $.post(
                PT_Ajax.ajaxurl, {
                    action: 'ajax-inputtitleSubmit',
                    numberofwelds: $('input[name=numberofwelds]').val(),
                    numberofconwelds: $('input[name=numberofconwelds]').val(),
                    nextNonce: PT_Ajax.nextNonce
                }, function(response) {
                    $('#totalone').val(response);
                });
                return false;
            });
        });
    })(jQuery);
});
更新


console.log
只显示我试图在
#totalone
字段中显示的数字。JS控制台中没有任何错误

创建Ajax响应以发送回Jquery的PHP:

 function myajax_inputtitleSubmit_func() {
        $nonce = $_POST['nextNonce'];   
    if ( ! wp_verify_nonce( $nonce, 'myajax-next-nonce' ) )
        die ( 'Busted!');

    $numwelds = isset($_POST['numberofwelds']) ? $_POST['numberofwelds'] : '';
$numconwelds = isset($_POST['numberofconwelds']) ? $_POST['numberofconwelds'] : '';

if (is_numeric($numwelds) && is_numeric($numconwelds))
{
    $total = $numwelds + $numconwelds;
    $response = json_encode($total);
    header("Content-Type: application/json");  
    echo $response;
    exit;
} 
}
表单HTML

<form action="" method="post" name="formsubmit" id="formsubmit"   >
<h1> Process </h1>
<p> operation type always robot </p>
Number of welds: <input type="number" name="numberofwelds" id="numberofwelds"  >
<br /> <br />
Number of construction welds: <input type="number" name="numberofconwelds" id="numberofconwelds"  >
<br /> <br />
Total one: <input type="text" name="totalone" id="totalone" disabled>
<br /> <br />
<input type="submit"  value="Calculate" id="submit" name="submit" class="ajax-link" >
<div id="result"> </div>
</form>

过程
操作型机器人

焊缝数量:

施工焊缝数量:

共1个:

试试这个。将val()更改为text()


我在你的代码中发现了一些额外的括号等等。以下是可能正常工作的已清理版本:

(function($) {
  $(function() {
    $('#formsubmit').click(function(){
        $.post(
            PT_Ajax.ajaxurl,
            {
                action : 'ajax-inputtitleSubmit',
                numberofwelds : $('input[name=numberofwelds]').val(),
                numberofconwelds : $('input[name=numberofconwelds]').val(),
                nextNonce : PT_Ajax.nextNonce
            },
            function( response ) {
                $("#totalone").val(response);
            }
        );
        return false;
    });
  });
}(jQuery));

我在这里更改了单引号
$('#totalone').val(响应)
这样加倍
$(“#totalone”).val(响应)。它现在似乎起作用了&我不太明白它为什么起作用

请发布日志输出。
console.log
只显示我试图在
#totalone
字段中显示的数字,JS控制台中没有任何错误。@lshetty我尝试了你的代码,但这似乎也不起作用。Jquery是针对wordpress的,所以我认为额外的括号是必要的。谢谢anyway@mattnewbie,jQuery就是jQuery,无论在哪里使用它。你真的需要2“jQuery(document).ready(function($){)”吗?你在控制台日志中看到了什么?这就是你设置字段值的方式:。控制台中有javascript错误吗?你有没有记录
响应的值
?你确定
$('#totalone'))
选择元素?控制台中没有任何错误。当我将
$('#totalone').val(响应)替换为
console.log(响应)时`数字显示在控制台中。
#totalone
是id,我也把它放在上面了。你也可以发布你的表单/HTML吗?@LShetty它工作了!!非常感谢。我把单引号改成了双引号。有点困惑为什么这样行,对吗?我的答案?如果是,请接受:)