Django和js中的傳參方法


var curr = $(this)
var id_list = curr.attr('id').split('$$')
var id0 = id_list[0];
var id1 = id_list[1];
var action = curr.text() //還可以從html文本中獲取參數信息、
var status_tag = curr.parent().siblings(".join_team_status").first(); //獲取上級標籤的方法
$.ajax(
{
    url:'home/',
    type: "post",
    data:{ "type":"join_team_reply",
           "id1":"id1" },
    dataType :"json"
    success: function(data)
    {
        if (data.result == 0)
        {
            curr.text('Pending');
            curr.removeAttr("id");//下次點擊不再有反應
            curr.removeAttr("class");//同上
        } else
        {
            alert("In team already");
        }
    }
}
);
//django中  if request.is_ajax() and request.method == 'POST' :

#django中函數post返回值的寫法
result = 0
return HttpResponse(json.dumps({'result':result}),content_type='application/json')

html中標籤的技巧
<td class="" > </td>
<i class="" > </i>

#django render html批量傳參的技巧 (字典、列表的嵌套)
d_list=[]
d = {}
attr_list = []
attr_list.append({'left':'hahaha1', 'right':'xixixi1'})
attr_list.append({'left':'hahaha2', 'right':'xixixi2'})
d['attr_list']=attr_list
d_list.append(d)
 

//下拉框方法  默認值方法
<select class="bar0 ef" id="match_select">
    {% for match in match_list %}
        <option value="{{match.id}}"> {{ match.match_name }} </option>
        <option value="公里">公里</option>
        <option value="米" selected="selected">米</option>
        <option value="釐米">釐米</option>
    {% endfor %}
</select>


//還有文檔操作的技巧 重點是發送一個post之後回調函數的寫法
<p>$.post(
    window.location.pathname,
    {
        "operation_type"    : "get_event_list",
        "sport_id"            : $("#sport_select").first().val(),
        "match_id"            : $("#match_select").first().val()
    },
    function(data, status) {
        $("#event_select").first().empty();
        $.each(data.event_list, function(n, value) {
            $("#event_select").first().append("<option value='" + value.id + "'>" + value.event_name + "</option>");
        });
    }
);
</p>//對應的data的形式爲
event_list_p = []
    for event in event_list :
        d = {}
        d["id"] = str(event.id)
        d["event_name"] = event.event_name
        event_list_p.append(d)
    return HttpResponse(
        json.dumps({
            "event_list" : list(event_list_p)
        }),
        content_type = "application/json"
    )
小結:傳的參數都是列表中套字典,地點中的對象還可以是列表(render的時候)
其實json就是一個包含字典的列表。
傳json的時候json.dumps({"event_list:list(event_list_p)"})


<pre name="code" class="javascript"><pre name="code" class="javascript">//求js動態改變post內容的方法(或許也可以用貼標籤的方法解決)
//改變post或者是改變ajax的內容
//可以嘗試對$.ajax中的data內容進行修改,data就是一個簡單的dict嗎?<p>//問題解決! 對詞典動態複製發現一個很厲害的函數:eval()</p><p>data = {}</p><p>key = 'key'</p><p>value = 'value'</p><p>eval('data.'+key+'='+value)
</p>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章