简体   繁体   English

如何使用javascript动态更改HTML值

[英]How to change the HTML Value dynamically using javascript

My Problem is in brief below: This is done using PHP, JS, Smarty and HTML 我的问题在下面简要介绍: 这是使用PHP,JS,Smarty和HTML完成的

    <form name="todaydeal" method="post">
        <span id="fix_addonval"></span>
        <input type="radio" name="offer" id="offer_{$smarty.section.mem.index+1}" value="{$display_offers[mem].offervalue}" onclick="return sel_bees(this.value);" />
<input type="submit" name="SET" value="SET" />
        </form>
        {literal}
        <script>
        function sel_bees(Val)
        {
            document.getElementById('fix_addonval').innerHTML='<input type="hidden" name="addon" id="addon" value='+Val+' />'
        }
        </script>

    {/literal}

When i click on the radio button its value must be replaced via javascript innerHTML and it should be placed in the span id. 当我单击单选按钮时,必须通过javascript innerHTML替换其值,并将其放置在span ID中。 When i submit the form using submit button the value which is replaced in the should be get in the next page. 当我使用“提交”按钮提交表单时,应在下一页中获取替换的值。 How can this be done ? 如何才能做到这一点 ?

Any ideas will be thankful and grateful. 任何想法都会感激和感激。 Thanks in advance 提前致谢

This approach looks convoluted for me. 对我来说,这种方法令人费解。 All you are trying to do is to create a dynamic hidden variable and assign a value to if the checkbox is checked. 您要做的就是创建一个动态隐藏变量,并为该复选框分配一个值。 Ideally, I would keep the hidden variable in the markup and will assign the value to it if the checkbox is checked. 理想情况下,如果选中此复选框,则将隐藏变量保留在标记中,并为其分配值。 Since it is hidden, it is not going affect your HTML markup. 由于它是隐藏的,因此不会影响您的HTML标记。

我不知道span元素是否应该包含文本以外的其他任何东西,至少是XHTML方式,并且如果输入的类型是隐藏的,它可以安全地驻留在页面中并替换该值。

Try it with jQuery. 尝试使用jQuery。

http://jsfiddle.net/vXgzK/ http://jsfiddle.net/vXgzK/

Your code is so buggy ! 您的代码是如此错误! Your question is not clear, but if you need to create a text box in the span using javascript you can do the following: 您的问题尚不清楚,但是如果您需要使用javascript在跨度中创建一个文本框,则可以执行以下操作:

some mistakes you've made in your code 您在代码中犯的一些错误
1) you said, you need to create a textbox so you should use (text) as the type of your input. 1)您说过,您需要创建一个文本框,因此应使用(文本)作为输入类型。
2) use (;) at the end of the javascript statement to run successfully in all browsers 2)使用javascript语句结尾的(;)在所有浏览器中成功运行
3) use < script type="text/javascript"> for your javascript code, to make it more readable 3)为您的JavaScript代码使用<script type =“ text / javascript”>,以使其更具可读性
4) you've forgot to enclose the value attribute in (") and I do it in the following code for you 4)您忘了将value属性包含在(“)中,而我将在以下代码中为您完成

jQuery: jQuery的:

$('fix_addonval').html('< input type="text" id="addon" value="' + Val + '"/>'); $('fix_addonval')。html('<输入类型=“ text” id =“ addon” value =“'+ Val +'” />');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM