[英]How can I swap the text inside html button, using a js function with id parameter?
我想在html按钮中交换文本,但使用具有3个参数的函数,即id,first_text,second_text。
我一直在寻找另一个具有相同问题的问题,但这对我没有帮助。
这不起作用:
Button(var1,value,id是php中的变量):
<input type='button' id=".$id." onclick='swaptxt('"$.id."', '".$var1."','".$value."')' value=".$value.">
功能:
function swaptxt(index, text1, text2){
var elem = document.getElementById(index);
if (elem.value===text1)
elem.value = text2;
else
elem.value = text1;
您没有获得正确的输出,因为您在参数中传递了错误的ID。 在输入中,您将id指定为“ 。$ id。 ”,但在参数中,您将id传递为“ $ .id。 ”。 提供正确的ID,您将获得预期的输出。
function swaptxt(index, text1, text2){ var elem = document.getElementById(index); if (elem.value===text1) elem.value = text2; else elem.value = text1; }
<input type='button' id=".$id." onclick='swaptxt(".$id.",".$value.", ".$var1.")' value=".$value">
不要从PHP中回显它,而是退出PHP,仅使用PHP添加变量。
?> <!-- we are no longer in PHP -->
<input type='button' id='<?php echo $id; ?>'
onclick='swaptxt('<?php echo $id; ?>',
'<?php echo $var1; ?>',
'<?php echo $value; ?>')'
value='<?php echo $value; ?>'>
<?php // back to PHP now
进行连接的另一种方法:
onclick="swaptxt(\'' . $id . '\',\'' . $var1 . '\',\'' . $value .'\');"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.