javascript 在onClick函数中传递输入文本框值 - php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28718052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:25:45  来源:igfitidea点击:

Pass input textbox value in onClick function - php

javascriptphpjqueryhtml

提问by Matarishvan

I have a input text field. I need to pass the values entered inside the element through onClick of javascript.

我有一个输入文本字段。我需要通过 javascript 的 onClick 传递在元素内输入的值。

<form>
<fieldset>     
  <label>Common Site ID: </label><span><?php echo $commonsiteid ?></span><br>
  <label>Acres: </label><input id="acre_value" name="acre_value" type="text" value="<?php echo $acre; ?>">
 </fieldset>
</form>
<input type="submit" value="submit" onclick="saveValue('<?php echo $_REQUEST['acre_value'] ?>')">

I am passing the value through submit onClick, Which is going empty. How do i pass value in this onclick function.

我正在通过提交 onClick 传递值,该值将变为空。我如何在这个 onclick 函数中传递值。

回答by Ataboy Josef

Try this:

试试这个:

<input type="submit" value="submit" onclick="saveValue(document.getElementById('acre_value').value);">

回答by GuiPab

I try this, and worked:

我尝试这个,并工作:

<script>
    function saveValue(){
        alert(document.formName.hiddenField.value);
        /*do something*/
        return false;
    }
</script>
<form name="formName" method="post">
    <input type="hidden" name="hiddenField" value="<?php echo $_REQUEST['acre_value'] ?>"/>
    <input type="submit" value="submit" onclick="saveValue()">
</form>

As you can see, i pass the value by a hidden field, and on the Js function i get the value of this field.
If you need a php function instead off a js, it's the same logic.

如您所见,我通过隐藏字段传递值,并在 Js 函数上获取该字段的值。
如果你需要一个 php 函数而不是一个 js,这是相同的逻辑。

回答by Nirav Kamani

Have you tried writing something like following.

您是否尝试过编写类似以下内容的内容。

<input type="submit" value="submit" onclick="saveValue(document.getElementById('acre_value').value)">