【问题标题】:How Can I do for manipulate html tags inside of a template in jquery tmpl?如何在 jquery tmpl 中操作模板内的 html 标签?
【发布时间】:2012-07-02 21:35:33
【问题描述】:

我有一个问题,我无法从 jquery 模板外部访问以操作 html 代码。 结果如下:[]

我有这个模板:

<script id="tmpl" type="text/x-jquery-tmpl">
<select id="country">
<?php
$query = 'SELECT country_id, country_name FROM countries';
$results = $wpdb->get_results($query, OBJECT);
foreach($results as $result)
{
echo '<option value="'.$result->country_id.'">'.$result->country_name.'</option>';
}
?>
</select>
<span value="${countryHelpers(user.country)}" />
</script>

在模板之外我有这段代码 javascript

<script type="text/javascript">
function countryHelpers(country) 
{
$("#country option[value='"+country+"']").attr("selected",function(){return "selected";});
}

如何将 attr “选中”

谢谢!

【问题讨论】:

  • 你不能在脚本标签中使用html标签。

标签: php jquery jquery-plugins


【解决方案1】:

你可以试试这个:

function countryHelpers(country) 
{
    $("#country option").filter(function() {
      return $(this).text() === country
    }).attr('selected', true);
}

编辑: 还有一个类似的问题:Selecting an option in a dropdown menu based on node value?

【讨论】:

    猜你喜欢
    • 2011-08-28
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2011-08-18
    相关资源
    最近更新 更多