【问题标题】:Passing a variable into an other function ( jeasyui )将变量传递给另一个函数(easyui)
【发布时间】:2014-10-15 16:37:58
【问题描述】:

您好,我一直在拼命地尝试在函数之间传递变量,我尝试了一些解决方案,但没有奏效——也许我做错了。我不知道如何解决这个问题,这对你们中的一些人来说可能是微不足道的,期待帮助。

它是如何工作的:输入单选应该在 url 中添加一个参数(queryParams - 根据文档应该没问题),我称之为显示指定数据的附加过滤器.当您单击单选脚本时,将获取该值并将其附加到 queryParams。

如果您有更多问题,请不要犹豫。

<div class="adv_filter">
    <li>
        <input type="radio" name="letter_type" data-custom="Radio1" value="0">Radio1</li>
    <li>
        <input type="radio" name="letter_type" data-custom="Radio2" value="1" checked="checked">Radio2</li>
    <li>
        <input type="radio" name="letter_type" data-custom="Radio3" value="2">Radio3</li>
</div>

$("li").on("click", "input", function () {
    $('#dg').datagrid('reload'); //reloads the grid
    alert($(this).val()); //gets the value of input type radio
    globalVar = $(this).val(); //assign the value to a global variable
    $('#string').html(globalVar);
});

$('#dg').datagrid({

    url: 'data_loader.php',
    queryParams: { //this adds to the url ?ltype=valur
        ltype: globalVar //assign the global variable here
    }
});
//$('#string2').html(globalVar);

JSFiddle example.

【问题讨论】:

  • 您只调用一次.datagrid。它通过了 globalVar 当时的任何值。它不会神奇地被重新评估,除非你真的再次调用它
  • #dg 的代码立即执行。在click 之后不会执行它。所以globalVar 在页面加载时被传递,但再也不会传递。
  • $('#dg').datagrid('reload'); 这一行是你需要通过globalVar的地方。
  • 您可以这样做吗:jsfiddle.net/rmt0r563/4 当您单击每个单选按钮时,它似乎正在触发请求(从控制台判断)。
  • @user3793639:转换为堆栈 sn-p 并发布在下面。

标签: javascript jquery jquery-easyui


【解决方案1】:

一种解决方案是再次致电.datagrid

$("li").on("click", "input", function () {
    var val = $(this).val();
    $('#string').html(val);
    updateDataGrid(val);
});

updateDataGrid();   // this initial call may or may not be needed. 
                    // if the grid should load something on start up
                    // then pass a parameter to it here.

function updateDataGrid(query) {
    $('#dg').datagrid({

        url: 'data_loader.php',
        queryParams: { 
            ltype: query 
        }
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
<link href="http://www.jeasyui.com/easyui/themes/default/easyui.css" rel="stylesheet"/>

<hr>Filter
<hr>
<div class="adv_filter">
    <li>
        <input type="radio" name="letter_type" data-custom="Radio1" value="0">Radio1</li>
    <li>
        <input type="radio" name="letter_type" data-custom="Radio2" value="1" checked="checked">Radio2</li>
    <li>
        <input type="radio" name="letter_type" data-custom="Radio3" value="2">Radio3</li>
</div>
<hr>
<table class="easyui-datagrid" id="dg" title="Basic DataGrid" style="width:700px;height:250px" data-options="singleSelect:true,collapsible:true">
    <thead>
        <tr>
            <th data-options="field:'itemid',width:80">Item ID</th>
            <th data-options="field:'productid',width:100">Product</th>
            <th data-options="field:'listprice',width:80,align:'right'">List Price</th>
            <th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
            <th data-options="field:'attr1',width:250">Attribute</th>
            <th data-options="field:'status',width:60,align:'center'">Status</th>
        </tr>
    </thead>
</table>
<hr>
<!-- debug -->inside
<div id="string"></div>
<hr>outer
<div id="string2"></div>

【讨论】:

    猜你喜欢
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多