从表单元素之外的输入标签读取 [英] reading from input tag outside form element
问题描述
我有一个带有输入字段的网页(从日历中选择一个日期)。
问题是我需要从两个不同的表单元素中读取输入字段,每个表单元素都有自己的行动和其他价值观。
|日历| [做这个] [这样做]
我不想复制两种形式的输入字段,因为它看起来很傻。
有什么建议吗?
编辑:这里有两种形式:
pre> < form action =/ adm / user / credit / update?sms =<?= $ id?>& user =<?= $ user?> 方法= POST >
< input type =hiddenname =isPaidvalue =1/>
< label for =datums>基准:< / label>
< input type =textid =datumsname =datums/>
< input type =submitvalue =Paidstyle =background-color:green; color:white/>
< / form>
< form action =/ adm / user / credit / extend?sms =<?= $ id?>& user =<?= $ user?>方法= POST >
< input type =hiddenname =phonevalue =<?= $ phone?>>
< select name =period>
< option value =7> 7< / option>
< option value =14> 14< / option>
< option value =30> 30< / option>
< / select> days
< input type =submitvalue =Extendstyle =background-color:blue; color:white>
< / form>
只需给它唯一的ID并使用 document.getElementById
就像你想要的任何地方:
var myValue = document.getElementById( myInputID)值。
这样, 编辑: 现在在您的页面中有这样的内容: 这会将该值显示为警报对话框。如果您想使用该值填充隐藏的表单字段,代之以这样的代码: 希望它足够清楚! :) I have a web page with an input field (a calendar to select a date from). I don't want to copy the input field in both forms since it would look silly.
Any suggestions? Edit: here's the two forms:
Just give it unique ID and use This way it doesn't matter where the Edit: And now have this in your page: This will show the value as alert dialog.. if you want to populate hidden form field with the value, have such code instead: Hope it's clear enough! :) 这篇关于从表单元素之外的输入标签读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!输入$ c $
要提交时读取值,首先添加 onsubmit
部分到相关表格:
< form action =.. ...onsubmit =ReadValue(this);>
< script type =text / javascript>
函数ReadValue(oForm){
var myValue = document.getElementById(myInputID).value;
alert(value is:+ myValue);
}
< / script>
$ b $ pre $ 函数ReadValue(oForm){
var myValue = document.getElementById(myInputID )。值;
oForm.elements [myHiddenFieldId]。value = myValue;
}
The problem is that I need to read that input field from 2 different form elements that each have their own action and other values.
|calendar| [do this] [do that]
<form action="/adm/user/credit/update?sms=<?=$id?>&user=<?=$user?>" method="post">
<input type="hidden" name="isPaid" value="1"/>
<label for="datums">Datums: </label>
<input type="text" id="datums" name="datums"/>
<input type="submit" value="Paid" style="background-color:green; color:white"/>
</form>
<form action="/adm/user/credit/extend?sms=<?=$id?>&user=<?=$user?>" method="post">
<input type="hidden" name="phone" value="<?=$phone?>">
<select name="period">
<option value="7">7</option>
<option value="14">14</option>
<option value="30">30</option>
</select> days
<input type="submit" value="Extend" style="background-color: blue; color: white">
</form>
document.getElementById
like this anywhere you want:var myValue = document.getElementById("myInputID").value;
input
is located, it can even be outiside of any form.
To read the value upon submit, first add the onsubmit
part to the relevant form:<form action="....." onsubmit="ReadValue(this);">
<script type="text/javascript">
function ReadValue(oForm) {
var myValue = document.getElementById("myInputID").value;
alert("value is: " + myValue);
}
</script>
function ReadValue(oForm) {
var myValue = document.getElementById("myInputID").value;
oForm.elements["myHiddenFieldId"].value = myValue;
}