将文本字符串值传递给 hiddenbform 值字段以进行发布



>将文本字符串值传递到隐藏的表单值字段以进行发布... 这是我的代码..

Onmouseover="goto function();"
Function gotofunction (){ document.getelementbyid 
('icon').src="icon2.png";
Var chosen="mobiles"; document.getelementbyid ('value').value = chosen;
Ondblclick = function () {document.getelementbyid ('send').submit (); 
return false; }
<Form id="send" action="Page.php" target="iframe" method="post">
<input id="choice" type="hidden" type="text" name="genres" value=" "/>
<input type="hidden" type="submit" value="post">
</form>

我的问题是将所选变量传递给值字段以发送到 php 页面....我可以为每个具有静态值的盛开图标进行硬编码,但是将字符串文本传递给该值是明智的。 Iv尝试了从php echo到createattributes的大量方法,但我失败得很糟糕。.

如果有人有解决方法将简单的字符串值传递给值字段,那么您真的值得感谢。

你需要小心JavaScript,因为它区分大小写...而且您还需要确保您正在使用的 ID 存在(您也可以轻松测试这一点,但现在让我们让事情运行......

var target = document.getElementById('mytarget');
target.addEventListener("mouseenter", gotofunction);
target.ondblclick = function () { document.getElementById('send').submit(); }
function gotofunction() {
document.getElementById('icon').src = "icon2.png";
var chosen = "mobiles";
document.getElementById('choice').value = chosen;
}

而 HTML ...

<p id="mytarget">
Target?
</p>
<img id="icon" />
<form id="send" action="Page.php" target="iframe" method="post">
<input id="choice" type="hidden" type="text" name="genres" value=" "/>
<input type="hidden" type="submit" value="post">
</form>

如果您希望所选变量来自 PHP...你可以这样做:

var chosen = "<?= $chosen ?>";

或者这更简单...

<input id="choice" type="hidden" type="text" name="genres" value="<?= $chosen ?>" />;

在这两种情况下,您都需要确保字符串中没有引号。

最新更新