使用 Jquery 或 Javascript 将隐藏字段值设置为 Javascript 变量

发布于 2024-08-17 23:57:32 字数 3468 浏览 6 评论 0 原文

非常简短的背景:

我正在使用 Jquery Autocomplete 来查找来自数据库的项目。然后,该值以某种方式赋予同一表单中的隐藏字段,然后插入到数据库中。

让这件事变得稍微复杂的是我正在使用 Jquery Ui Tabs,我过去对它并没有太多的兴趣。

文件中创建选项卡的一些代码:

<script type="text/javascript">
function findValue(li) {
    // if( li == null ) return alert("No match!");

    // if coming from an AJAX call, let's use the CityId as the value
    if( !!li.extra ) var sValue = li.extra[0];

    // otherwise, let's just display the value in the text box
    else var sValue = li.selectValue;
}

function selectItem(li) {
    findValue(li);
}

function formatItem(row) {
    return row[0];
}

function lookupAjax(){
    var oSuggest = $(".role")[0].autocompleter;

    oSuggest.findValue();

    return false;
}

function lookupLocal(){
    var oSuggest = $("#role")[0].autocompleter;

    oSuggest.findValue();

    return false;
}
</script>

同一个文件创建选项卡,并且还有一个启动 Jquery 自动完成的回调

<script type="text/javascript">
        $(function() {
            $("#tabs").tabs({
             load: function(event, ui) { setTimeout( function() { $(".title").focus(); }, 500 );
                    var ac = $(".role").autocomplete(
                        "/profile/autocomplete",
                        {
                            delay:10,
                            minChars:1,
                            matchSubset:1,
                            matchContains:1,
                            cacheLength:10,
                            onItemSelect:selectItem,
                            onFindValue:findValue,
                            formatItem:formatItem,
                            autoFill:true
                        }
                    );
                    
                    ac[0].autocompleter.findValue(); 
                    }
                });
            });
        </script>

然后在实际的选项卡代码中是表单

<?php   $tab_id = $this->uri->segment(4);
    $hidden = array('tab_id' => $tab_id);
    $attributes = array('name' => 'additem');
echo form_open('profile/new_item', $attributes, $hidden); ?>
<input type="hidden" value="" name="rolehidden"/>
<?php echo validation_errors(); ?>
<table width="100%" padding="0" class="add-item">
    <tr>
        <td>Title: <input class="title" type="text" name="title" value="<?php echo set_value('title'); ?>"></input></td>
        <td>Role: <input class="role" type="text" name="role" size="15"></input></td>
        <td>Year: <input type="text" name="year" size="4" maxlength="4"></input></td>
        <td align="right"><input type="submit" value="Add"></input></td>
    </tr>
</table>
</form>t 

我想要做的就是获取 sValue 并使其成为表单中隐藏字段的值。

我还应该提到,JQuery 选项卡有多个选项卡,它们都具有相同的 值形式。这意味着所有选项卡中有多个不同的输入字段都具有相同的名称/id/类。

我知道这是 ID 属性的问题,但不确定是否同样适用于名称属性。

我已经尝试了很多不同的 Javascript 和 Jquery 代码片段,以至于我无法再思考了。

突破...但仍然是一个问题

$("[name='rolehidden']").val(sValue);

刚刚取得突破。此代码确实有效...但仅适用于 元素。它无法在 上工作 有解决办法吗?或者我应该使用 CSS 来隐藏文本输入框?

请帮助

蒂姆

Very Brief Background:

I am using Jquery Autocomplete to lookup the the value of an item from a database. That value is then somehow given to a hidden field within the same form and then inserted to the database.

What complicates this slightly is that I am working through Jquery Ui Tabs, which I haven't had a lot of fun with in the past.

So some code within the file that creates the tabs:

<script type="text/javascript">
function findValue(li) {
    // if( li == null ) return alert("No match!");

    // if coming from an AJAX call, let's use the CityId as the value
    if( !!li.extra ) var sValue = li.extra[0];

    // otherwise, let's just display the value in the text box
    else var sValue = li.selectValue;
}

function selectItem(li) {
    findValue(li);
}

function formatItem(row) {
    return row[0];
}

function lookupAjax(){
    var oSuggest = $(".role")[0].autocompleter;

    oSuggest.findValue();

    return false;
}

function lookupLocal(){
    var oSuggest = $("#role")[0].autocompleter;

    oSuggest.findValue();

    return false;
}
</script>

The same file creates the tabs and also has a callback initiating the Jquery Autocomplete

<script type="text/javascript">
        $(function() {
            $("#tabs").tabs({
             load: function(event, ui) { setTimeout( function() { $(".title").focus(); }, 500 );
                    var ac = $(".role").autocomplete(
                        "/profile/autocomplete",
                        {
                            delay:10,
                            minChars:1,
                            matchSubset:1,
                            matchContains:1,
                            cacheLength:10,
                            onItemSelect:selectItem,
                            onFindValue:findValue,
                            formatItem:formatItem,
                            autoFill:true
                        }
                    );
                    
                    ac[0].autocompleter.findValue(); 
                    }
                });
            });
        </script>

Then in the actual tab code is the form

<?php   $tab_id = $this->uri->segment(4);
    $hidden = array('tab_id' => $tab_id);
    $attributes = array('name' => 'additem');
echo form_open('profile/new_item', $attributes, $hidden); ?>
<input type="hidden" value="" name="rolehidden"/>
<?php echo validation_errors(); ?>
<table width="100%" padding="0" class="add-item">
    <tr>
        <td>Title: <input class="title" type="text" name="title" value="<?php echo set_value('title'); ?>"></input></td>
        <td>Role: <input class="role" type="text" name="role" size="15"></input></td>
        <td>Year: <input type="text" name="year" size="4" maxlength="4"></input></td>
        <td align="right"><input type="submit" value="Add"></input></td>
    </tr>
</table>
</form>t 

All I want to do is get sValue and make it the value of the hidden field in the form.

I should also mention that the JQuery Tabs have multiple tabs that all have the same form. This means that there are several different input fields all with the same name/id/class across all of the tabs.

I know this is a problem for ID attributes, but not sure if the same applies to the name attributes.

I have tried so many different code snippets of Javascript and Jquery that I just can't think anymore.

BREAKTHROUGH ... But still a problem

$("[name='rolehidden']").val(sValue);

Just had a break through. This code does work... BUT only on an <input ="text"> Element. It fails to work on <input ="hidden"> Is there a work around or should I use CSS to hide the text input box?

Please help

Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千紇 2024-08-24 23:57:32

“我想要做的就是获取 sValue 并使其成为表单中隐藏字段的值。”

你尝试过吗?

$("[name='rolehidden']").val(sValue);

"All I want to do is get sValue and make it the value of the hidden field in the form."

Have you tried?

$("[name='rolehidden']").val(sValue);
(り薆情海 2024-08-24 23:57:32

试试这个

$("[name='rolehidden']").val(sValue);

Try This

$("[name='rolehidden']").val(sValue);
罪歌 2024-08-24 23:57:32

尝试 $('input[name=rolehidden]').val(sValue);

Try $('input[name=rolehidden]').val(sValue);

感情废物 2024-08-24 23:57:32

您可以通过 $(selector).attr(key, value); 来完成此操作,

例如:

$('input.classname').attr('value','value to set');

You can do this by $(selector).attr(key, value);

eg :

$('input.classname').attr('value','value to set');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文